/**
 * deel with the BBS_Class`s affairs
 */
jQuery(function($) {
	$(function getBBSBase() {
		$.ajax({
			type : "post",
			url : "GetForumBaseServlet",
			data : "",
			dateType : "json",
			success : function(data) {
				var jsondata = eval('(' + data + ')');
				var baseNode = $("#chart").append("<p class=\"chart z\"></p>");
				baseNode.children().append(
						"今日: <em>" + jsondata.tNum
								+ "</em><span class=\"pipe\">|</span>昨日: <em>"
								+ jsondata.yNum + "</em><span"
								+ "class=\"pipe\">|</span>帖子: <em>"
								+ jsondata.allNum
								+ "</em><span class=\"pipe\">|</span>会员:"
								+ "<em>" + jsondata.uNum + "</em>");
			},
			error : function() {
				// TODO do something

			}
		});
	});
});

// show BBS_Class Table
jQuery(function($) {
	$(function getBBSClass() {
		$
				.ajax({
					type : "post",
					url : "GetBBSSpecialServlet",
					data : "",
					dateType : "json",
					success : function(data) {
						var isSM = isSuperManager();
						var jsondata = eval('(' + data + ')');
						var content_div, bk, newTitle_div, newZTcontent_div;
						var newTableNode;
						content_div = $("#bk_content");
						if (isSM) {
							content_div
									.append("<div class=\"bm bmw  flg cl\" style=\"border-bottom-width: 0px; border-top-width: 0px; margin-bottom: 10px; margin-top: 10px;\"><button class=\"pn pnc vm\" onclick=\"showTools('ins','0','0')\"><strong>添加模块</strong></button> </div>");
						}
						$
								.each(
										jsondata.content,
										function(bknum, bkcontent) {
											// 专题
											bk = $("<div> </div>").addClass(
													"bm bmw  flg cl").attr(
													"id", "bk" + bknum);
											// 专题标题
											newTitle_div = $("<div></div>")
													.addClass("bm_h cl");
											newTitle_div
													.append("<span class=\"o\"> <img id=\"category_"
															+ bknum
															+ "_img\" src=\"../images/collapsed_no.gif\" title=\"收起 /展开\" alt=\"收起 /展开\" onclick=\"toggle_collapse(\'category_"
															+ bknum
															+ "\');\" /> </span><span class=\"y\" style=\"margin-right: 10px;\"><a id=\"insertZT\" class=\"notabs\" onclick=\"showTools('inc','"
															+ bkcontent.ID
															+ "','"
															+ bkcontent.Name
															+ "')\" href=\"javascript:;\">添加</a></span> <span class=\"y\" style=\"margin-right: 8px;\"><a id=\"deletZT\" class=\"notabs\" onclick=\"postDZt('"
															+ bkcontent.ID
															+ "','0')\" href=\"javascript:;\">删除</a></span><h2> <a style=\"\" href=\"#\">"
															+ bkcontent.Name
															+ "</a> </h2>");
											if (!isSM) {
												newTitle_div.find("#insertZT")
														.remove();
												newTitle_div.find("#deletZT")
														.remove();
											}

											bk.append(newTitle_div);
											// 这里处理专题分类
											newZTcontent_div = $(
													"<div style=\"\" ><div>")
													.addClass("bm_c")
													.attr("id",
															"category_" + bknum);
											// 取出版块中专题
											newTableNode = getBBSSpecial(bkcontent.ID);
											newZTcontent_div
													.append(newTableNode);
											bk.append(newZTcontent_div);
											content_div.append(bk);
										});

					},
					error : function() {
						// TODO do something

					}
				});
	});

	function getBBSSpecial(BBSSpecial_id) {
		var newTableNode, newTbodyNode, newTrNode, newTdLNode, newTdRNode;
		// 表格内部
		newTableNode = $("<table cellpadding=\"0\" cellspacing=\"0\"></table>")
				.addClass("fl_tb");
		newTbodyNode = $("<tbody></tbody>");
		$
				.ajax({
					type : "post",
					url : "GetBBSClassServlet",
					data : {
						BBSSpecial_id : BBSSpecial_id
					},
					dateType : "json",
					success : function(data2) {
						var jsondata2 = eval('(' + data2 + ')');
						$
								.each(
										jsondata2.content,
										function(ztnum, ztcontent) {
											var name = encodeURIComponent(ztcontent.Name);
											// name=encodeURIComponent(url);
											newTdLNode = $(
													"<td width=\"50%\"></td>")
													.addClass("fl_g");
											newTdLNode
													.append("<div class=\"fl_icn_g\" style=\"width: 48px;\">"
															+ "<a href=\""
															+ getRootPath()
															+ "/community/forum_zt.html?zt={id:"
															+ ztcontent.ID
															+ "}\"><img "
															+ "src=\""
															+ getRootPath()
															+ "/"
															+ ztcontent.ImageUrl
															+ "\" alt=\"\" align=\"left\" /></a> </div>"
															+ "<dl style=\"margin-left: 48px;width: 300px;\">"
															+ "<dt> <a href=\""
															+ getRootPath()
															+ "/community/forum_zt.html?zt={id:"
															+ ztcontent.ID
															+ "}\">&nbsp;"
															+ ztcontent.Name
															+ "</a><em class=\"xw0 xi1\" title=\""
															+ ztcontent.Name
															+ "\"></em> </dt>"
															+ "<dd  style=\"padding-top: 4px; padding-bottom: 4px;\"><p class=\"xg2\">"
															+ ztcontent.Intro
															+ "</p></dd>"
															+ "<dd> <em>主题: "
															+ ztcontent.TopicCount
															+ "</em>, <em>回复数: "
															+ ztcontent.ReplyCount
															+ "</em> </dd>"
															+ "</dl>");
											newTrNode = $("<tr></tr>").append(
													newTdLNode);
											newTrNode
													.append("<td width=\"45%\" class=​\"fl_icn\"></td>");
											if (isSuperManager()) {
												newTrNode
														.append("<td width=\"5%\" class=​\"fl_icn\"><div ><a class=\"xi2\" onclick=\"postDZt('"
																+ ztcontent.ID
																+ "','1')\" href=\"javascript:;\" >删除</a></div></td>");
											}
											if (ztnum != 0) {
												newTrNode.addClass("fl_row");
											}
											newTbodyNode.append(newTrNode);
										});
						newTableNode.append(newTbodyNode);

					},
					error : function() {
						// TODO do something
					}
				});
		return newTableNode;
	}
	function isSuperManager() {
		var aaa = false;

		var uRight = $.cookie('uRight');
		if (uRight == null || uRight == '') {
			return false;
		}
		var jsonDate = eval("(" + uRight + ")");
		if (jsonDate.uarray.length != 0) {
			for (var i = 0; i < jsonDate.uarray.length; i++) {
				if (jsonDate.uarray[i].ClassID == '0') {
					aaa = true;
				}
			}
		}
		return aaa;
	}
	function getURightJson() {
		return jsonDate;
	}
	// js获取项目根路径，如： http://localhost:8080/ems
	function getRootPath() {
		// 获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
		var curWwwPath = window.document.location.href;
		// 获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
		var pathName = window.document.location.pathname;
		var pos = curWwwPath.indexOf(pathName);
		// 获取主机地址，如： http://localhost:8080
		var localhostPath = curWwwPath.substring(0, pos);
		// 获取带"/"的项目名，如：/ems
		var projectName = pathName.substring(0,
				pathName.substr(1).indexOf('/') + 1);
		return (localhostPath + projectName);
	}
	postDZt = function(id, tyle) {
		if (id) {
			if (confirm("将执行 删除，并且无法恢复。您确定吗？")) {
				$.ajax({
					type : "post",
					url : "DeletZtServlet",
					data : {
						id : id,
						tyle : tyle
					},
					dateType : "json",
					success : function(data) {
						var jsondata = eval('(' + data + ')');
						if (jsondata.isSuccess == "true") {
							window.location.reload();
						} else {
							confirm("删除失败，请再次操作！！")
						}
					},
					error : function() {
						// TODO do something
					}
				});
			}
		} else {
			alert("请选择 你想要操作的选项!!");
		}
	}
});